package com.jianrui.msgvision.view.widget

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import androidx.annotation.LayoutRes
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.util.DisplayUtil

/**
 * Author: Delete
 * Date: 2019/10/18 11:40
 * Description: Splash 协议 拒绝 Dialog
 *
 */
class ProtocolRejectDialog : Dialog {
    companion object {
        val default_layoutRes = R.layout.dialog_protocol_reject
    }

    var positiveButtonMsg: String? = null
    var positionListener: DialogInterface.OnClickListener? = null
    @LayoutRes
    private var layotuResId: Int? = null
    var contentAction: ContentAction? = null


    constructor(builder: Builder) : super(builder.context, R.style.WholeSaleDialogTheme) {
        this.positiveButtonMsg = builder.positiveButtonMsg
        this.positionListener = builder.positiveListener
        this.layotuResId = builder.layoutResId
        this.contentAction = builder.contentAction
        init()
    }


    fun init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        val contentView = LayoutInflater.from(context)
            .inflate(layotuResId ?: default_layoutRes, null, false)
        if (layotuResId != default_layoutRes)
            contentAction?.onDialogViewCreated(this, contentView)
        setContentView(contentView)
        setCancelable(false)
        val confirmBtn = contentView.findViewById<TextView>(R.id.confirm_tv)
        confirmBtn.text = positiveButtonMsg ?: "确定"
        positionListener?.let { positionListener ->
            confirmBtn.setOnClickListener {
                positionListener.onClick(this, confirmBtn.id)
            }
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val window = window
        val lp = window!!.attributes
        //使用wrap login dialog 会存在 很窄的情况
//        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        val screenWidth = DisplayUtil.getScreenWidth(context)
        lp.width = (screenWidth * 0.72f).toInt()
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
//        lp.height = (DisplayUtil.getScreenWidth(context) * 0.34f).toInt()
        lp.dimAmount = 0.3f//背景透明度 0全透 1 全黑
        window.attributes = lp
    }


    class Builder {
        var positiveButtonMsg: String? = null
        var positiveListener: DialogInterface.OnClickListener? = null
        @LayoutRes
        var layoutResId: Int = default_layoutRes

        var contentAction: ContentAction? = null

        var context: Context

        constructor(context: Context) {
            this.context = context
        }

        fun build(): ProtocolRejectDialog = create()

        private fun create() = ProtocolRejectDialog(this)

    }

    interface ContentAction {
        fun onDialogViewCreated(dialog: Dialog, view: View)
    }
}